package com.jokerpan.project.controller;

import com.jokerpan.project.annotation.AccessLimit;
import com.jokerpan.project.annotation.LogAction;
import com.jokerpan.project.commons.ActionResult;
import com.jokerpan.project.commons.ListResult;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.entity.UsersRole;
import com.jokerpan.project.service.RolesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("roles")
@Api("角色操作Api")
@PreAuthorize("hasPermission('','admin:list')")
public class RolesController {

    @Resource
    RolesService rolesService;

    @GetMapping("/queryList")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("查询所有角色")
    @PreAuthorize("hasPermission('','admin:list')")
    public ListResult<Object> queryList(UsersRole usersRole) {
        return ResultBuilder.buildListSuccess(this.rolesService.queryList(usersRole));
    }

    @PostMapping("/add")
    @ApiOperation("新增角色")
    @AccessLimit(seconds = 6, maxCount = 5)
    @LogAction("角色管理,新增角色数据,新增角色,新增")
    @PreAuthorize("hasPermission('','admin:update')")
    public ActionResult addRole(@RequestBody UsersRole usersRole){
        if (this.rolesService.add(usersRole)){
            return ResultBuilder.buildActionSuccess();
        }else {
            return ResultBuilder.buildActionFailed();
        }
    }

    @GetMapping("/queryById")
    @AccessLimit(seconds = 6, maxCount = 5)
    @ApiOperation("根据id查询权限")
    public ListResult<Object> queryById(@RequestParam("id") Integer id){
        return ResultBuilder.buildListSuccess(this.rolesService.queryById(id));
    }

    @PostMapping("/update")
    @ApiOperation("修改角色")
    @AccessLimit(seconds = 6, maxCount = 5)
    @LogAction("角色管理,修改角色数据,修改角色,修改")
    @PreAuthorize("hasPermission('','admin:update')")
    public ActionResult update(@RequestBody UsersRole usersRole){
        return this.rolesService.update(usersRole);
    }

    @GetMapping("/delete")
    @ApiOperation("删除角色")
    @AccessLimit(seconds = 6, maxCount = 5)
    @LogAction("角色管理,删除角色数据,删除角色,删除")
    @PreAuthorize("hasPermission('','admin:delete')")
    public ActionResult delete(@RequestParam("id") Integer id){
        return this.rolesService.deleteById(id);
    }

}
